import { useLocalStorage } from '@vueuse/core'
import { defineStore } from 'pinia'
import { ref } from 'vue'

const useCommonStore = defineStore('common', () => {
  const navList = ref<MyNav[]>([])
  const visitedPages: Ref<MyNav[]> = useLocalStorage('pinia/common/visitedPages', [])

  function setNavList(list: MyNav[]) {
    navList.value = list
  }
  function getVisitedPages(): MyNav[] {
    if (visitedPages.value.length === 0 || visitedPages.value[0].path !== '/') {
      visitedPages.value.unshift({
        name: '首页',
        icon: 'i-ep-home-filled',
        path: '/',
      })
    }
    return visitedPages.value
  }
  function addVisitedPage(page: MyNav) {
    if (!visitedPages.value.some(item => item.path === page.path)) {
      visitedPages.value.push(page)
    }
  }
  function removeVisitedPage(path: string) {
    visitedPages.value = visitedPages.value.filter(item => item.path !== path)
  }
  return { navList, setNavList, visitedPages, getVisitedPages, addVisitedPage, removeVisitedPage }
})

export default useCommonStore
